package com.ktgj.train.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.train.domain.InterfaceEntity.TrRetreatTicketRequest;
import com.ktgj.train.domain.InterfaceEntity.TrRetreatTicketResponse;
import com.ktgj.train.domain.TrainCRMOrder;
import com.ktgj.train.domain.TrainChangeOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.train.domain.TrainOrderRefundrecord;
import com.ktgj.train.service.ITrainOrderRefundrecordService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 退票记录Controller
 * 
 * @author ruoyi
 * @date 2021-11-29
 */
@ControllerAdvice
@RestController
@RequestMapping("/orderRefundrecord")
public class TrainOrderRefundrecordController extends BaseController
{
    @Autowired
    private ITrainOrderRefundrecordService trainOrderRefundrecordService;

    /**
     * 查询退票记录列表
     */
    @PreAuthorize(hasPermi = "train:refundrecord:list")
    @GetMapping("/list")
    public TableDataInfo list(TrainOrderRefundrecord trainOrderRefundrecord)
    {
        startPage();
        List<TrainOrderRefundrecord> list = trainOrderRefundrecordService.selectTrainOrderRefundrecordList(trainOrderRefundrecord);
        return getDataTable(list);
    }

    /**
     * 查询退票记录列表
     *
     * @param orderId 退票记录
     * @return 退票记录集合
     */
    @GetMapping("/queryTrainOrderRefundrecordList")
    public List<TrainOrderRefundrecord> queryTrainOrderRefundrecordList(@RequestParam("orderId") Long orderId, @RequestParam("changeType") String changeType) {
        return trainOrderRefundrecordService.queryTrainOrderRefundrecordList(orderId, changeType);
    }

    /**
     * 导出退票记录列表
     */
    @PreAuthorize(hasPermi = "train:refundrecord:export")
    @Log(title = "退票记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainOrderRefundrecord trainOrderRefundrecord) throws IOException
    {
        List<TrainOrderRefundrecord> list = trainOrderRefundrecordService.selectTrainOrderRefundrecordList(trainOrderRefundrecord);
        ExcelUtil<TrainOrderRefundrecord> util = new ExcelUtil<TrainOrderRefundrecord>(TrainOrderRefundrecord.class);
        util.exportExcel(response, list, "退票记录数据");
    }

    /**
     * 获取退票记录详细信息
     */
    @PreAuthorize(hasPermi = "train:refundrecord:query")
    @GetMapping(value = "/{recordid}")
    public AjaxResult getInfo(@PathVariable("recordid") Long recordid)
    {
        return AjaxResult.success(trainOrderRefundrecordService.selectTrainOrderRefundrecordById(recordid));
    }

    /**
     * 新增退票记录
     */
    @PreAuthorize(hasPermi = "train:refundrecord:add")
    @Log(title = "退票记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainOrderRefundrecord trainOrderRefundrecord)
    {
        return toAjax(trainOrderRefundrecordService.insertTrainOrderRefundrecord(trainOrderRefundrecord));
    }

    /**
     * 修改退票记录
     */
    @PreAuthorize(hasPermi = "train:refundrecord:edit")
    @Log(title = "退票记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainOrderRefundrecord trainOrderRefundrecord)
    {
        return toAjax(trainOrderRefundrecordService.updateTrainOrderRefundrecord(trainOrderRefundrecord));
    }

    /**
     * 删除退票记录
     */
    @PreAuthorize(hasPermi = "train:refundrecord:remove")
    @Log(title = "退票记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordids}")
    public AjaxResult remove(@PathVariable Long[] recordids)
    {
        return toAjax(trainOrderRefundrecordService.deleteTrainOrderRefundrecordByIds(recordids));
    }

    /**
     * 查询改签记录列表
     * @param order
     * @return
     */
    @PostMapping("/queryTrainChangeOrderList")
    public TableDataInfo queryTrainChangeOrderList(@RequestBody TrainCRMOrder order) {
        startPage();
        List<TrainChangeOrderVO> orderList = trainOrderRefundrecordService.queryTrainChangeOrderList(order);
        return getDataTable(orderList);
    }

    /**
     * 改签详情
     * @param map
     * @return
     */
    @PostMapping("/queryTrainChangeOrder")
    public AjaxResult queryTrainChangeOrder(@RequestBody Map<String, Object> map) {
        String orderId = String.valueOf(map.get("orderId"));
        List<TrainChangeOrderVO> orderVOList = trainOrderRefundrecordService.queryTrainChangeOrder(orderId);
        return AjaxResult.success(orderVOList);
    }

    /**
     * 改签退票
     * @param request
     * @return
     */
    @PostMapping("/retreatTicket")
    public TrRetreatTicketResponse retreatTicket(HttpServletRequest servletRequest, @RequestBody TrRetreatTicketRequest request) {
        TrRetreatTicketResponse response;
        try {
            response = trainOrderRefundrecordService.retreatTicket(servletRequest, request);
        } catch (Exception e){
            e.getMessage();
            return null;
        }

        return response;
    }
}
